package com.ruoyi.youtian.controller;

import com.ruoyi.common.annotation.AutoSetDefaultParams;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdw;
import com.ruoyi.gzmx_scdw.domain.Gzmx_scdwVo;
import com.ruoyi.gzmx_scdw.domain.ZtreeNew;
import com.ruoyi.gzmx_scdw.service.IGzmx_scdwService;
import com.ruoyi.gzmx_scdw.service.impl.Gzmx_scdwServiceImpl;
import com.ruoyi.youtian.domain.TSceneCategory;
import com.ruoyi.youtian.domain.TSceneCategoryTreeSelect;
import com.ruoyi.youtian.domain.TreeSelect;
import com.ruoyi.youtian.domain.vo.ScenePointVo;
import com.ruoyi.youtian.domain.vo.SourceTagVo;
import com.ruoyi.youtian.domain.vo.TSceneCategoryAssignEquipmentVo;
import com.ruoyi.youtian.service.ITSceneCategoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 场景管理Controller
 *
 * @author ruoyi
 * @date 2022-09-12
 */
@Controller
@RequestMapping("/youtian/scene-category")
public class TSceneCategoryController extends BaseController
{
    private String prefix = "youtian/scene-category";

    @Autowired
    private ITSceneCategoryService tScenceCategoryService;

    @Autowired
    private IGzmx_scdwService gzmx_scdwService;

    @GetMapping()
    @RequiresPermissions("system:user:view")
    public String sceneCategory()
    {
        return prefix + "/scene-category";
    }

    /**
     * 查询场景列表
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TSceneCategory tScenceCategory)
    {
        startPage();
        List<TSceneCategory> list = tScenceCategoryService.selectTSceneCategoryList(tScenceCategory);
        return getDataTable(list);
    }



    /**
     * 查询场景列表（功能修改）
     */
    @GetMapping("/reolist")
    @ResponseBody
    public TableDataInfo reolist(TSceneCategory tScenceCategory)
    {
        startPage();
        List<TSceneCategory> list = tScenceCategoryService.selectTSceneCategoryreoList(tScenceCategory);
        return getDataTable(list);
    }

    @Autowired
    private Gzmx_scdwServiceImpl gzmxScdwService;
    /**
     * 查询场景树结构
     */
//    @GetMapping("/since-dept-tree")
    @GetMapping("/tree")
    @ResponseBody
    public AjaxResult tree(TSceneCategory tScenceCategory)
    {
       List<TSceneCategoryTreeSelect> tScenceCategoryTreeSelects = tScenceCategoryService.selectTSceneCategoryTreeList(tScenceCategory);
       return AjaxResult.success(tScenceCategoryTreeSelects);
    }

    @GetMapping("/ztree")
    @ResponseBody
    public List<ZtreeNew>  ztree(TSceneCategory tScenceCategory)
    {
        List<ZtreeNew> ztreeNewList = tScenceCategoryService.selectTSceneCategoryZtreeList(tScenceCategory);
        return ztreeNewList;
    }

    /**
     *  -- 场景管理
     * -- 1.左侧树中第一级、第二级采用组织架构中的区、站进行展示，并且在其之下自定义场景的定义
     */
    @PostMapping("/since-dept-tree")
//    @GetMapping("/tree")
    @ResponseBody
    public AjaxResult sinceDeptTree(TSceneCategory tScenceCategory)
    {
        List<TreeSelect> scdwTreeList = gzmx_scdwService.selectGzmx_scdwTreeSelectListNew(null);
        List<TreeSelect> list = tScenceCategoryService.selectScdwSceneTreeList(null, scdwTreeList);
        return AjaxResult.success(list);
    }

    /**
     * 获取场景详细信息
     */
    @GetMapping(value = "/{sceneId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("sceneId") Long sceneId)
    {
        return AjaxResult.success(tScenceCategoryService.selectTSceneCategoryBySceneId(sceneId));
    }

    /**
     * 新增场景
     */
    @Log(title = "场景", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult add(@RequestBody TSceneCategory tScenceCategory)
    {

        SysUser sysUser = getSysUser();
        List<TSceneCategory> tScenceCategoryList = tScenceCategoryService.selectTTTTSceneCategoryList(tScenceCategory);
        if(tScenceCategoryList != null && tScenceCategoryList.size() > 0 ){
            return AjaxResult.error("场景名称已存在");
        }
        tScenceCategory.setCreateBy(sysUser.getUserName());
        tScenceCategory.setDeptId(sysUser.getDeptId());
        tScenceCategory.setDeptName(sysUser.getDept().getDeptName());
        return toAjax(tScenceCategoryService.insertTSceneCategory(tScenceCategory));
    }



    /**
     * 修改场景
     */
    @Log(title = "场景", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult edit(@RequestBody TSceneCategory tScenceCategory)
    {
        tScenceCategory.setUpdateBy(getLoginName());
        return toAjax(tScenceCategoryService.updateTSceneCategory(tScenceCategory));
    }

    /**
     * 删除场景
     */
    @Log(title = "场景", businessType = BusinessType.DELETE)
	@RequestMapping("/{sceneId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long sceneId)
    {
        return toAjax(tScenceCategoryService.deleteTSceneCategoryBySceneId(sceneId, getLoginName()));
    }

    /**
     *  删除场景设备
     * @param sceneCategory
     * @return
     */
    @Log(title = "场景", businessType = BusinessType.UPDATE)
    @PutMapping("/assignEquipmentDel")
    @ResponseBody
    public AjaxResult assignEquipmentDel(@RequestBody TSceneCategoryAssignEquipmentVo sceneCategory){
        sceneCategory.setCreateBy(getLoginName());
        int i = tScenceCategoryService.assignEquipmentDel(sceneCategory);
        return  AjaxResult.success(i);
    }


    /**
     * 为场景分配设备
     * @param sceneCategory
     * @return
     */
    @Log(title = "场景", businessType = BusinessType.UPDATE)
    @PostMapping("/assignEquipmentAdd")
    @ResponseBody
    public AjaxResult assignEquipmentAdd(@RequestBody TSceneCategoryAssignEquipmentVo sceneCategory){
        sceneCategory.setCreateBy(getLoginName());
        int i = tScenceCategoryService.assignEquipmentAdd(sceneCategory);
        return  AjaxResult.success(i);
    }

    @GetMapping("/getChildSceneIdList")
    @ResponseBody
    public AjaxResult getChildSineIdList(Long categoryId){
        List<Long> childTSceneCategoryIdList = tScenceCategoryService.getChildTSceneCategoryIdList(categoryId);
        return AjaxResult.success(childTSceneCategoryIdList);
    }

    /**
     * 场景类型监测
     * @param scenePointVo
     * @return
     */
    @GetMapping("/scencePointlist")
    @ResponseBody
    public TableDataInfo scencePointlist(ScenePointVo scenePointVo)
    {
        if(scenePointVo != null && scenePointVo.getSceneId() == null){
            scenePointVo.setSceneId(-1L);
        }
        List<Long> childTSceneCategoryIdList = tScenceCategoryService.getChildTSceneCategoryIdList(scenePointVo.getSceneId());
        startPage();
        List<ScenePointVo> scenePointVoList = tScenceCategoryService.selectScenePointVoListBySceneIdList(childTSceneCategoryIdList, scenePointVo.getPointName(), scenePointVo.getSceneName());

        return getDataTable(scenePointVoList);
    }

    /**
     * 场景监测
     * 通过场景查询点位 不加分页
     * @param scenePointVo
     * @return
     */
    @GetMapping("/scencePointlistNoPage")
    @ResponseBody
    public TableDataInfo scencePointlistNoPage(ScenePointVo scenePointVo)
    {
        if(scenePointVo != null && scenePointVo.getSceneId() == null){
            scenePointVo.setSceneId(-1L);
        }
        List<Long> childTSceneCategoryIdList = tScenceCategoryService.getChildTSceneCategoryIdList(scenePointVo.getSceneId());
        List<ScenePointVo> scenePointVoList = tScenceCategoryService.selectScenePointVoListBySceneIdList(childTSceneCategoryIdList, scenePointVo.getPointName(), scenePointVo.getSceneName());
        return getDataTable(scenePointVoList);
    }

    @GetMapping("/getSceneCategory")
    @ResponseBody
    public AjaxResult getSceneCategoryList(Long categoryId){
        List<HashMap> sceneCategoryMonitorList = tScenceCategoryService.getSceneCategoryMonitorList(categoryId);
        return  AjaxResult.success(sceneCategoryMonitorList);
    }

    @GetMapping("/getSceneCategoryAll")
    @ResponseBody
    public AjaxResult getSceneCategoryAllList(){
        List<HashMap> sceneCategoryMonitorList = tScenceCategoryService.getSceneCategoryMonitorListAll();
        return  AjaxResult.success(sceneCategoryMonitorList);
    }


    @GetMapping("/getSceneCategoryChildIdList")
    @ResponseBody
    public AjaxResult getSceneCategoryChildIdList(Long categoryId){
        List<TSceneCategory> childCategoryList = tScenceCategoryService.getChildCategoryList(categoryId);
        return  AjaxResult.success(childCategoryList);
    }

    @PostMapping("/selectSourceTagHistoryListBySourceTag")
    @ResponseBody
    public AjaxResult getSourceTagList(@RequestBody SourceTagVo sourceTagVo){
        List<SourceTagVo> sourceTagVoList = tScenceCategoryService.selectSourceTagHistoryListBySourceTag(sourceTagVo);
        return  AjaxResult.success(sourceTagVoList);
    }



    /**
     * 场景管理结构树
     * @param tScenceCategory
     * @return
     */
//    @DataScope(deptAlias = "d")
    @PostMapping("/since-dept-tree-new")
    @ResponseBody
    public AjaxResult sinceDeptTreeNew(TSceneCategory tScenceCategory)
    {
        List<TreeSelect> scdwTreeList = gzmx_scdwService.selectGzmx_scdwTreeSelectListNew(null);
        List<TreeSelect> list = tScenceCategoryService.selectScdwSceneTreeList(null, scdwTreeList);
        return AjaxResult.success(list);
    }
}
